﻿using GrainManage.Entities.Contact;
using GrainManage.Web.Common;
using GrainManage.Web.Models;
using GrainManage.Web.Models.Contact;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace GrainManage.Web.Controllers
{
    public class ContactController : BaseController
    {
        public ActionResult SearchContacts()
        {
            return View();
        }

        public ActionResult NewContact()
        {
            return View();
        }

        public ActionResult EditContact(int contactID, string status = "")
        {
            var input = new InputGetByID
            {
                Authentication = CurrentAccount,
                ContactID = contactID
            };
            var contactProxy = ObjectFactory.ContactProxy;
            var output = contactProxy.GetByID(input);
            var contact = output.Contact;
            var model = new UpdateContactModel();
            if (contact != null)
            {
                model.ContactID = contact.ContactID;
                model.Address = contact.Address;
                model.Area = contact.Area;
                model.BirthDate = contact.BirthDate;
                model.Cellphone = contact.Cellphone;
                model.ContactName = contact.ContactName;
                model.Email = contact.Email;
                model.Gender = contact.Gender;
                model.ImageID = contact.ImageID;
                model.QQ = contact.QQ;
                model.Remarks = contact.Remarks;
                model.Telephone = contact.Telephone;
                ViewBag.Message = status;
            }
            else
            {
                ViewBag.Message = output.Status.Message;
            }

            return View(model);
        }

        [HttpPost]
        public ActionResult GetContactName(string query, PageModel page)
        {
            var inputPara = new InputSearchName
            {
                Authentication = CurrentAccount,
                Name = Filter.MatchPart(query),
                PageIndex = page.pageIndex,
                PageSize = page.pageSize
            };
            var contactProxy = ObjectFactory.ContactProxy;
            var result = contactProxy.SearchName(inputPara);
            if (Request.IsAjaxRequest())
            {
                return NewtonsoftJson(result.Contacts);
            }
            return PartialView(Constant.DropDownListView, result.Contacts);
        }

        [HttpPost]
        public ActionResult GetContactArea(string query, PageModel page)
        {
            var inputPara = new InputSearchArea
            {
                Authentication = CurrentAccount,
                Area = Filter.MatchPart(query),
                PageIndex = page.pageIndex,
                PageSize = page.pageSize
            };
            var contactProxy = ObjectFactory.ContactProxy;
            var result = contactProxy.SearchArea(inputPara);
            if (Request.IsAjaxRequest())
            {
                return NewtonsoftJson(result.Areas);
            }
            return PartialView(Constant.DropDownListView, result.Areas);
        }

        [HttpPost]
        public ActionResult GetContactAddress(string query, PageModel page)
        {
            var inputPara = new InputSearchAddress
            {
                Authentication = CurrentAccount,
                Address = Filter.MatchPart(query),
                PageIndex = page.pageIndex,
                PageSize = page.pageSize
            };
            var contactProxy = ObjectFactory.ContactProxy;
            var result = contactProxy.SearchAddress(inputPara);
            if (Request.IsAjaxRequest())
            {
                return NewtonsoftJson(result.Addresses);
            }
            return PartialView(Constant.DropDownListView, result.Addresses);
        }

        [HttpPost]
        public ActionResult SearchContacts(string address, string area, string name, PageModel page)
        {
            var inputPara = new InputSearch
            {
                Authentication = CurrentAccount,
                PageIndex = page.pageIndex,
                PageSize = page.pageSize,
                IsOr = false,
                ContactFilter = new Data.Model.Filter.ContactFilter
                {
                    Address = Filter.MatchPart(address),
                    Area = Filter.MatchWhole(area),
                    Name = Filter.MatchPart(name)
                }
            };
            var contactProxy = ObjectFactory.ContactProxy;
            var result = contactProxy.Search(inputPara);
            return PartialView("_ContactList", result);
        }

        [HttpPost]
        public ActionResult Search(string query, bool isName, bool isArea, bool isAddress, PageModel page)
        {
            if (!string.IsNullOrEmpty(query))
            {
                var inputPara = new InputSearch
                {
                    Authentication = CurrentAccount,
                    PageIndex = page.pageIndex,
                    PageSize = page.pageSize,
                    IsOr = true,
                    ContactFilter = new Data.Model.Filter.ContactFilter
                    {
                        Address = isAddress ? Filter.MatchPart(query) : null,
                        Area = isArea ? Filter.MatchPart(query) : null,
                        Name = isArea ? Filter.MatchPart(query) : null
                    }
                };
                var contactProxy = ObjectFactory.ContactProxy;
                var result = contactProxy.Search(inputPara);
                return PartialView("_ContactList", result);
            }
            return Content(string.Empty);
        }

        [HttpPost]
        public ActionResult Insert(AddContactModel input)
        {
            if (!ModelState.IsValid)
            {
                return View(input);
            }
            var inputPara = new InputInsert
            {
                Authentication = CurrentAccount,
                Contact = new Data.Model.Table.ContactTable
                {
                    Address = input.Address,
                    Area = input.Area,
                    BirthDate = input.BirthDate,
                    Cellphone = input.Cellphone,
                    ContactName = input.ContactName,
                    Email = input.Email,
                    Gender = input.Gender,
                    ImageID = input.ImageID,
                    QQ = input.QQ,
                    Remarks = input.Remarks,
                    Telephone = input.Telephone
                }
            };
            var contactProxy = ObjectFactory.ContactProxy;
            var result = contactProxy.Insert(inputPara);
            if (Request.IsAjaxRequest())
            {
                return NewtonsoftJson(result);
            }
            return RedirectToAction("EditContact", new { contactID = result.ContactID, status = result.Status.Message });
        }

        [HttpPost]
        public ActionResult Update(UpdateContactModel input)
        {
            if (!ModelState.IsValid)
            {
                return View(input);
            }
            var inputPara = new InputUpdate
            {
                Authentication = CurrentAccount,
                Contact = new Data.Model.Table.ContactTable
                {
                    ContactID = input.ContactID,
                    Address = input.Address,
                    Area = input.Area,
                    BirthDate = input.BirthDate,
                    Cellphone = input.Cellphone,
                    ContactName = input.ContactName,
                    Email = input.Email,
                    Gender = input.Gender,
                    ImageID = input.ImageID,
                    QQ = input.QQ,
                    Remarks = input.Remarks,
                    Telephone = input.Telephone
                }
            };
            var contactProxy = ObjectFactory.ContactProxy;
            var result = contactProxy.Update(inputPara);
            if (Request.IsAjaxRequest())
            {
                return NewtonsoftJson(result);
            }
            return RedirectToAction("EditContact", new { contactID = input.ContactID, status = result.Status.Message });
        }

        [HttpPost]
        public ActionResult Delete(int contactID)
        {
            var inputPara = new InputDelete
            {
                Authentication = CurrentAccount,
                ContactID = contactID
            };
            var contactProxy = ObjectFactory.ContactProxy;
            var result = contactProxy.Delete(inputPara);
            if (Request.IsAjaxRequest())
            {
                return NewtonsoftJson(result);
            }
            return View(Constant.RestStatusView, result);
        }
    }
}
